import os

def remove_spaces_in_image_names(directory):
    # 支持的图片扩展名
    image_extensions = ('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.tiff', '.webp')

    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.lower().endswith(image_extensions):
                old_path = os.path.join(root, file)
                new_name = file.replace(' ', '')  # 去除空格
                new_path = os.path.join(root, new_name)

                if new_name != file:
                    try:
                        os.rename(old_path, new_path)
                        print(f"Renamed: {old_path} -> {new_path}")
                    except Exception as e:
                        print(f"Error renaming {old_path}: {e}")

if __name__ == '__main__':
    file_dir = '/Users/binny/Downloads/商城材料'
    target_directory = file_dir # 替换为你的目标路径
    remove_spaces_in_image_names(target_directory)
